package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/11 21:29
 * https://leetcode-cn.com/problems/perfect-squares/
 */
public class Title279 {

    /**
     *
     * dp[j]表示和为j的完全平方数的最小数量
     * 递推公式：dp[j] = Math.min(dp[j], dp[j - nums[i]] +1)
     * 初始化：dp[0] = 0 dp[1] = 1
     * @param n
     * @return
     */
    public int numSquares(int n) {
        int[] dp = new int[n + 1];
        //初始化
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i < dp.length; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        //物品个数
        for (int i = 1; i <= Math.sqrt(n); i++) {
            for (int j = i * i; j <= n; j++) {
                if ((j - i * i) != Integer.MAX_VALUE) {
                    dp[j] = Math.min(dp[j], dp[j - i * i] + 1);
                }
            }
        }
        return dp[n];
    }

    public static void main(String[] args) {

    }
}
